package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBKnowCategoryService;
import com.example.knowman.ObService.OBknowLabelDetailService;
import com.example.knowman.ReturnMessage.MExKnowCategory9_2LabelNew;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.labelCategoryWithStaffNAndKnowCategoryN;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.labelCategory;
import com.example.knowman.entity.rootList;
import com.example.knowman.entity.staffList;
import com.example.knowman.mapper.LabelCategoryMapper;
import com.example.knowman.mapper.RootListMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IStaffListService;
import com.example.knowman.service.IlabelCategoryService;
import com.example.knowman.service.IrootListService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LabelCategoryServiceImpl extends ServiceImpl<LabelCategoryMapper, labelCategory> implements IlabelCategoryService {

    private final LabelCategoryMapper labelCategoryMapper;
    private final StaffListMapper staffListMapper;

    private final IStaffListService iStaffListService;

    private final IKnowCategoryService iKnowCategoryService;

    private final OBknowLabelDetailService oBknowLabelDetailService;

    private final OBKnowCategoryService obKnowCategoryService;

    public LabelCategoryServiceImpl(LabelCategoryMapper labelCategoryMapper,StaffListMapper staffListMapper,IStaffListService iStaffListService
            ,IKnowCategoryService iKnowCategoryService,OBknowLabelDetailService oBknowLabelDetailService,OBKnowCategoryService obKnowCategoryService) {
        this.iStaffListService = iStaffListService;
        this.iKnowCategoryService = iKnowCategoryService;
        this.labelCategoryMapper = labelCategoryMapper;
        this.staffListMapper = staffListMapper;
        this.oBknowLabelDetailService = oBknowLabelDetailService;
        this.obKnowCategoryService = obKnowCategoryService;
    }


    @Override
    public IPage<labelCategory> getLabels(Integer pageNum, Integer pageSize, String labelName) {
        IPage<labelCategory> page = new Page<>(pageNum, pageSize);
        return this.lambdaQuery()
                .select()
                .like(labelName !=null && !labelName.isEmpty(),labelCategory::getLabelName,labelName)
                .orderByAsc(labelCategory::getLabelCateId)
                .page(page);
    }

    @Override
    public IPage<labelCategoryWithStaffNAndKnowCategoryN> getLabelsNew(Integer pageNum, Integer pageSize, String labelName,String staffCode) {
        IPage<labelCategoryWithStaffNAndKnowCategoryN> Respage = new Page<>(pageNum, pageSize);
        List<labelCategoryWithStaffNAndKnowCategoryN> recordsL = new ArrayList<>();
        IPage<labelCategory> page = new Page<>(pageNum, pageSize);
        page = this.lambdaQuery().select()
                .like(labelName !=null && !labelName.isEmpty(),labelCategory::getLabelName,labelName)
                .eq(staffCode !=null && !staffCode.isEmpty(),labelCategory::getStaffCode,staffCode)
//                .orderByAsc(labelCategory::getLabelCateId)
                .page(page);

        Respage.setTotal(page.getTotal());
        Map<String,String> SL = iStaffListService.getIdAndName();
//        System.out.println(SL);
        Map<Integer,String> KCL = iKnowCategoryService.getIdAndName();

        //根据knowCategory一级二级三级类弄个团一起的顺序

        //<knowCateId,顺序>
        Map<Integer,Integer> KCgo = obKnowCategoryService.getGo();
//        System.out.println(KCgo);
//        return null;
        //

        for (labelCategory item:page.getRecords()){
            labelCategoryWithStaffNAndKnowCategoryN temp = new labelCategoryWithStaffNAndKnowCategoryN();

            temp.setLabelCateId(item.getLabelCateId());
            temp.setLabelName(item.getLabelName());
            temp.setKnowCateId(item.getKnowCateId());
            temp.setKnowCateName(KCL.get(item.getKnowCateId()));
            temp.setLabelColor(item.getLabelColor());
            temp.setLabelSort(item.getLabelSort());
            temp.setNotes(item.getNotes());
            temp.setStaffCode(item.getStaffCode());
            temp.setStaffName(SL.get(item.getStaffCode()));
            temp.setCreatetime(item.getCreatetime());

            recordsL.add(temp);
        }

        //从小到大排
        recordsL.sort((o1, o2) -> KCgo.get(o1.getKnowCateId()).compareTo(KCgo.get(o2.getKnowCateId())));
        Respage.setRecords(recordsL);
//        Respage.setTotal(recordsL.size());
        return Respage;
    }

    @Override
    public Message Insert(String labelName, Integer knowCateId, String labelColor, Integer labelSort, String notes, String staffCode) {
        Message message = new Message();

        labelCategory temp = new labelCategory();
        temp.setLabelName(labelName);
        temp.setKnowCateId(knowCateId);
        temp.setLabelColor(labelColor);
        temp.setLabelSort(labelSort);
        temp.setNotes(notes);
        temp.setStaffCode(staffCode);

        try{
            this.save(temp);
            message.setCode(200);
            message.setTip("新增成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }

        return message;
    }

    @Override
    public Message update(Integer labelCateId, String labelName, Integer knowCateId, String labelColor, Integer labelSort, String notes) {
        Message message = new Message();
        this.lambdaUpdate().eq(labelCategory::getLabelCateId,labelCateId)
                .set(labelName != null && !labelName.isEmpty(),labelCategory::getLabelName,labelName)
                .set(knowCateId != null,labelCategory::getKnowCateId,knowCateId)
                .set(labelColor != null && !labelColor.isEmpty(),labelCategory::getLabelColor,labelColor)
                .set(labelSort != null,labelCategory::getLabelSort,labelSort)
                .set(notes != null && !notes.isEmpty(),labelCategory::getNotes,notes)
                .update();

        message.setCode(200);
        message.setTip("修改成功");

        return message;
    }

    @Override
    public Message deleteBylabelCateId(Integer labelCateId) {

        Integer nums = oBknowLabelDetailService.backNumBylabelCateId(labelCateId);
        Message message = new Message();

        if(nums > 0){
            message.setCode(100);
            message.setTip("该标签正在被使用！");
        }
        else {
            QueryWrapper<labelCategory> wrapper = new QueryWrapper<>();
            wrapper.eq("\"labelCateId\"",labelCateId);
            try{
                this.remove(wrapper);
                message.setCode(200);
                message.setTip("删除成功");
            }catch (Exception e){
                message.setCode(100);
                message.setTip("删除失败");
            }
        }
        return message;
    }

    @Override
    public Integer returnContainKnowCateIdCount(Integer knowCateId) {
        return this.lambdaQuery().select().eq(labelCategory::getKnowCateId,knowCateId).count();
    }


}
